package org.linlinjava.litemall.admin.service.order_manage;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.admin.form.*;
import org.linlinjava.litemall.admin.vo.order_manage.*;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.dao.app.AppHomeMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.AgedmallTopicDataService;
import org.linlinjava.litemall.db.service.AgedmallTopicImgService;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.vo.app.HomeGoodsListVo;
import org.linlinjava.litemall.db.vo.app.StoreGoodsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AgedmallHomeSettingService {
    @Resource
    private LitemallAdMapper litemallAdMapper;
    @Resource
    private AgedmallSalePointMapper agedmallSalePointMapper;
    @Resource
    private AgedmallTopicDataMapper agedmallTopicDataMapper;
    @Resource
    private AgedmallTopicImgMapper agedmallTopicImgMapper;
    @Resource
    private LitemallTopicMapper litemallTopicMapper;
    @Resource
    private LitemallGoodsService litemallGoodsService;
    @Resource
    private AppHomeMapper appHomeMapper;

    @Transactional
    public Object addAd(HomeAddAdListForm homeAddAdListForm){
        List<HomeAddAdForm> homeAddAdForms = homeAddAdListForm.getList();
        List<Integer> noDelIds = homeAddAdForms.stream().filter(p -> null != p.getId()).map(p -> p.getId()).collect(Collectors.toList());

        LitemallAdExample delExample = new LitemallAdExample();
        LitemallAdExample.Criteria criteria = delExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (noDelIds.size() > 0) {
            criteria.andIdNotIn(noDelIds);
        }
        litemallAdMapper.logicalDeleteByExample(delExample);

        for (HomeAddAdForm homeAddAdForm : homeAddAdForms) {
            LitemallAd litemallAd = new LitemallAd();
            litemallAd.setName(homeAddAdForm.getName());
            litemallAd.setLink(homeAddAdForm.getLink());
            litemallAd.setUrl(homeAddAdForm.getUrl());
            litemallAd.setCityType(homeAddAdForm.getCityType());
            litemallAd.setEventType(homeAddAdForm.getEvent());
            litemallAd.setCitys(JSONObject.toJSONString(homeAddAdForm.getCitys()));

            if (null != homeAddAdForm.getId()) {
                litemallAd.setId(homeAddAdForm.getId());
                litemallAd.setUpdateTime(LocalDateTime.now());
                litemallAdMapper.updateByPrimaryKeySelective(litemallAd);
            } else {
                litemallAd.setAddTime(LocalDateTime.now());
                litemallAdMapper.insertSelective(litemallAd);
            }
        }

        return ResponseUtil.ok();
    }

    public Object adList(){
        LitemallAdExample example = new LitemallAdExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<LitemallAd> litemallAds = litemallAdMapper.selectByExample(example);
        List<HomeAddAdVo> homeAddAdVos = litemallAds.stream().map(HomeAddAdVo::new).collect(Collectors.toList());
        return ResponseUtil.ok(homeAddAdVos);
    }

    @Transactional
    public Object addSalePoint(HomeAddSalePointListForm homeAddSalePointListForm){
        List<HomeAddSalePointForm> homeAddSalePointForms = homeAddSalePointListForm.getList();
        List<Integer> noDelIds = homeAddSalePointForms.stream().filter(p -> null != p.getId()).map(p -> p.getId()).collect(Collectors.toList());

        AgedmallSalePointExample delExample = new AgedmallSalePointExample();
        AgedmallSalePointExample.Criteria criteria = delExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (noDelIds.size() > 0) {
            criteria.andIdNotIn(noDelIds);
        }
        agedmallSalePointMapper.logicalDeleteByExample(delExample);

        for (HomeAddSalePointForm homeAddSalePointForm : homeAddSalePointForms) {
            AgedmallSalePoint agedmallSalePoint = new AgedmallSalePoint();
            agedmallSalePoint.setName(homeAddSalePointForm.getName());
            agedmallSalePoint.setUrl(homeAddSalePointForm.getUrl());

            if (null != homeAddSalePointForm.getId()) {
                agedmallSalePoint.setId(homeAddSalePointForm.getId());
                agedmallSalePoint.setUpdateTime(LocalDateTime.now());
                agedmallSalePointMapper.updateByPrimaryKeySelective(agedmallSalePoint);
            } else {
                agedmallSalePoint.setAddTime(LocalDateTime.now());
                agedmallSalePointMapper.insertSelective(agedmallSalePoint);
            }
        }

        return ResponseUtil.ok();
    }

    public Object salePointList(){
        AgedmallSalePointExample example = new AgedmallSalePointExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<AgedmallSalePoint> agedmallSalePoints = agedmallSalePointMapper.selectByExample(example);
        List<HomeAddSalePointVo> homeAddSalePointVos = agedmallSalePoints.stream().map(HomeAddSalePointVo::new).collect(Collectors.toList());
        return ResponseUtil.ok(homeAddSalePointVos);
    }

    public Object addRecommendPic(HomeAddRecommendPicForm homeAddRecommendPicForm){
        AgedmallTopicImgExample imgExample = new AgedmallTopicImgExample();
        imgExample.createCriteria().andDeletedEqualTo(false)
                .andTypeEqualTo(homeAddRecommendPicForm.getType());
        AgedmallTopicImg topicImg = agedmallTopicImgMapper.selectOneByExample(imgExample);
        if (null == topicImg) {
            topicImg = new AgedmallTopicImg();
            topicImg.setType(homeAddRecommendPicForm.getType());
            topicImg.setUrl(homeAddRecommendPicForm.getUrl());
            topicImg.setTitle(homeAddRecommendPicForm.getTitle());
            topicImg.setAddTime(LocalDateTime.now());
            agedmallTopicImgMapper.insertSelective(topicImg);
        } else {
            topicImg.setUrl(homeAddRecommendPicForm.getUrl());
            topicImg.setTitle(homeAddRecommendPicForm.getTitle());
            topicImg.setUpdateTime(LocalDateTime.now());
            agedmallTopicImgMapper.updateByPrimaryKeySelective(topicImg);
        }
        return ResponseUtil.ok();
    }

    public Object addRecommend(HomeAddRecommendForm homeAddRecommendForm){
        AgedmallTopicImgExample imgExample = new AgedmallTopicImgExample();
        imgExample.createCriteria().andDeletedEqualTo(false)
                .andTypeEqualTo(homeAddRecommendForm.getType());

        AgedmallTopicDataExample dataDelExample = new AgedmallTopicDataExample();
        dataDelExample.createCriteria().andDeletedEqualTo(false)
                .andTypeEqualTo(homeAddRecommendForm.getType());
        agedmallTopicDataMapper.logicalDeleteByExample(dataDelExample);

        for (Integer goodsId : homeAddRecommendForm.getGoodsIds()) {
            AgedmallTopicData topicData = new AgedmallTopicData();
            topicData.setType(homeAddRecommendForm.getType());
            topicData.setStoreGoodsId(goodsId);
            topicData.setAddTime(LocalDateTime.now());
            agedmallTopicDataMapper.insertSelective(topicData);
        }
        return ResponseUtil.ok();
    }

    public Object recommendPic(String type){
        HomeAddRecommendVo homeAddRecommendVo = new HomeAddRecommendVo();
        homeAddRecommendVo.setType(type);

        AgedmallTopicImgExample imgExample = new AgedmallTopicImgExample();
        imgExample.createCriteria().andDeletedEqualTo(false)
                .andTypeEqualTo(type);
        AgedmallTopicImg topicImg = agedmallTopicImgMapper.selectOneByExample(imgExample);
        if (null != topicImg) {
            homeAddRecommendVo.setUrl(topicImg.getUrl());
            homeAddRecommendVo.setTitle(topicImg.getTitle());
        }

        return ResponseUtil.ok(homeAddRecommendVo);
    }

    public Object recommend(String type){
        HomeAddRecommendVo homeAddRecommendVo = new HomeAddRecommendVo();
        homeAddRecommendVo.setType(type);
        List<HomeGoodsListVo> goodsListVos = appHomeMapper.getRecommendByType(type);
        homeAddRecommendVo.setGoodsList(goodsListVos);

        return ResponseUtil.ok(homeAddRecommendVo);
    }

    public Object addTopic(HomeAddTopicForm homeAddTopicForm){
        LitemallTopicExample example = new LitemallTopicExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andTypeEqualTo(homeAddTopicForm.getType());
        LitemallTopic topic = litemallTopicMapper.selectOneByExample(example);
        if (null == topic) {
            topic = new LitemallTopic();
            topic.setType(homeAddTopicForm.getType());
            topic.setTitle(homeAddTopicForm.getTitle());
            topic.setSubtitle(homeAddTopicForm.getSubTitle());
            topic.setPicUrl(homeAddTopicForm.getUrl());
            topic.setPicTitle(homeAddTopicForm.getPicTitle());
            topic.setGoods(StringUtils.join(homeAddTopicForm.getGoodsIds(), ","));
            topic.setAddTime(LocalDateTime.now());
            litemallTopicMapper.insertSelective(topic);
        } else {
            topic.setTitle(homeAddTopicForm.getTitle());
            topic.setSubtitle(homeAddTopicForm.getSubTitle());
            topic.setPicUrl(homeAddTopicForm.getUrl());
            topic.setPicTitle(homeAddTopicForm.getPicTitle());
            topic.setGoods(StringUtils.join(homeAddTopicForm.getGoodsIds(), ","));
            topic.setUpdateTime(LocalDateTime.now());
            litemallTopicMapper.updateByPrimaryKeyWithBLOBs(topic);
        }
        return ResponseUtil.ok();
    }

    public Object topic(Integer type){
        HomeAddTopicVo homeAddTopicVo = new HomeAddTopicVo();
        homeAddTopicVo.setType(type);

        LitemallTopicExample example = new LitemallTopicExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andTypeEqualTo(type);
        LitemallTopic topic = litemallTopicMapper.selectOneByExampleWithBLOBs(example);
        if (null != topic) {
            homeAddTopicVo.setTitle(topic.getTitle());
            homeAddTopicVo.setSubTitle(topic.getSubtitle());
            homeAddTopicVo.setUrl(topic.getPicUrl());
            homeAddTopicVo.setPicTitle(topic.getPicTitle());
            String[] goodsArr = topic.getGoods().split(",");
            List<Integer> goodsIds = Arrays.stream(goodsArr).map(Integer::parseInt).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(goodsIds)) {
                List<HomeGoodsListVo> goodsList = appHomeMapper.getTopicGoodsList(goodsIds);
                homeAddTopicVo.setGoodsList(goodsList);
            }
        }

        return ResponseUtil.ok(homeAddTopicVo);
    }

    public Object goodsList(Integer categoryId, Integer subCategoryId, String type, String searchKey, Integer page, Integer limit){
        List<HomeGoodsListVo> list = litemallGoodsService.queryHomeGoodsList(categoryId, subCategoryId, type, searchKey, page, limit);
        PageInfo<HomeGoodsListVo> pagedList = PageInfo.of(list);
        JSONObject object = new JSONObject();
        object.put("rows", list);
        object.put("nowPage", page);
        object.put("totalRows", pagedList.getTotal());
        object.put("totalPage", pagedList.getPages());

        return ResponseUtil.ok(object);
    }
}
